package com.cu.employmentmanagementsystem.service.impl;


import com.cu.employmentmanagementsystem.common.result.R;
import com.cu.employmentmanagementsystem.dao.RecruitInformationDao;
import com.cu.employmentmanagementsystem.dao.StudentRecruitmentDao;
import com.cu.employmentmanagementsystem.dao.StudentVitaDao;
import com.cu.employmentmanagementsystem.entity.RecruitInformation;
import com.cu.employmentmanagementsystem.entity.StudentRecruitment;
import com.cu.employmentmanagementsystem.entity.StudentVita;
import com.cu.employmentmanagementsystem.service.StudentVitaService;
import com.cu.employmentmanagementsystem.util.CommonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class StudentVitaServiceImpl implements StudentVitaService {

    @Autowired
    private StudentVitaDao studentVitaDao;
    @Autowired
    private RecruitInformationDao recruitInformationDao;

    @Autowired
    private StudentRecruitmentDao studentRecruitmentDao;
    /**
     * 更新简历审核状态
     * @return int 影响行数
     */
    @Override
    public int updatePeviewStatus(StudentVita studentVita) {
        return studentVitaDao.updatePeviewStatus(studentVita);
    }
    /**
     * 更新招聘审核状态
     * @return int 影响行数
     */
    @Override
    public int updatePeviewStatus2(RecruitInformation recruitInformation) {
        //测试招聘审核用
        return recruitInformationDao.update(recruitInformation);
    }

    @Override
    public List<StudentVita> getAllUnJudgeVitas()
    {
        return this.studentVitaDao.getAllUnJudgeVitas();
    }

    @Override
    public List<RecruitInformation> getAllUnJudgeRecruits()
    {
        return this.recruitInformationDao.getAllUnJudgeRecruits();
    }

    /**
     * 发布简历，本质上是insert
     * @param studentVita
     * @return int 影响行数
     */
    @Override
    public R commitVita(StudentVita studentVita) {
        Long Student_Id = CommonUtils.getStudentId();
        if (Student_Id == null){
            return R.notLogin("请先登录");
        }
        studentVita.setStudentId(Student_Id );
        studentVita.setPeviewStatus(-1);
        int x = studentVitaDao.insert(studentVita);
        if(x == 1){
            return R.success("成功发布简历");
        }else{
            return R.error("发布简历失败");
        }
    }

    /**
     * 更新简历，和发布简历步骤类似
     * @param studentVita
     * @return int 影响行数
     */
    @Override
    public R updateVita(StudentVita studentVita) {
        Long studentId = CommonUtils.getStudentId();
        StudentVita vita = this.studentVitaDao.selectByStudentId(studentId);
        Long vitaId = vita.getId();
        List<StudentRecruitment> studentRecruitmentList = this.studentRecruitmentDao.queryByresumeId(vitaId);
        if(!studentRecruitmentList.isEmpty()){
            return R.error("该简历已被投递，不能修改");
        }
        studentVita.setStudentId(studentId);
        studentVita.setPeviewStatus(-1);
        int x = studentVitaDao.update(studentVita);
        if(x > 0){
            return R.success("成功更新简历");
        }else{
            return R.error("更新简历失败");
        }
    }

    @Override
    public StudentVita selectByStudentId(Long id) {

        return studentVitaDao.selectByStudentId(id);
    }
}
